import {observable, action} from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
import * as API_WFREPORT from '../../apis/efficiencyReport';

const getLabel = WeaLocaleProvider.getLabel;

export class FlowTimeAnalyseStore {
    @observable loading = false;

    @observable advanceSearchData = {};
    @observable advanceSearchPara = {
        dateType: 1,
        startDate: '',
        endDate: '',
        wftypeid: -1,
        workflowid: -1,
        workflowname: ''
    };
    iswf = false;
    @observable modalParams = {};
    @observable analyseData = {};

    constructor() {
        this.updateAdvanceSearchPara = this.updateAdvanceSearchPara.bind(this);
        this.initDatas = this.initDatas.bind(this);
        this.loadAnalyseData = this.loadAnalyseData.bind(this);
        this.reSet = this.reSet.bind(this);
        this.showModal = this.showModal.bind(this);
        this.updateModalPara = this.updateModalPara.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.goBack = this.goBack.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    initDatas() {
        this.loading = true;
        API_WFREPORT.getFlowTimeAnalyseSearchCondition({}).then(data => {
            this.advanceSearchData = data;
            this.loading = false;
            const {
                noRight = false, rightKey = '-100'
            } = data;
            if (!noRight) {
                this.updateAdvanceSearchPara({
                    rightKey
                })
            }
        });
    }

    loadAnalyseData() {
        this.loading = true;
        //分请求提高性能
        Promise.all([
            API_WFREPORT.getFlowTimeAnalyseCountData({...this.advanceSearchPara}).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            }),
            API_WFREPORT.getFlowTimeAnalyseEchartData({...this.advanceSearchPara}).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            })
        ]).then(result => {
            this.loading = false;
        });
    }

    updateAdvanceSearchPara(para) {
        let isload = true;
        let defaultInfo = {};
        for (let o in para) {
            if (this.advanceSearchPara[o] != para[o]) {
                if (o === 'dateType') {
                    if (para[o] == 6) {
                        isload = false;
                    } else {
                        defaultInfo.startDate = '';
                        defaultInfo.endDate = '';
                    }
                }
                if (o === 'startDate' && para[o] == '') isload = false;
            }
        }
        this.advanceSearchPara = {...this.advanceSearchPara, ...para, ...defaultInfo};
        if (isload) {
            this.loadAnalyseData();
        }
    }

    reSet() {
        this.iswf = false;
        this.module = 1;
        this.advanceSearchPara = {
            dateType: 1,
            startDate: '',
            endDate: '',
            wftypeid: -1,
            workflowid: -1,
            workflowname: ''
        };
    }

    //type 1:更多 2:reqlist 3:count
    showModal(type, para = {}) {
        if (type == 1) {
            const {isOperatorChart = false} = para;
            const dataParams = {
                'api': 'getFlowTimeAnalyseDetailList',
                ...this.advanceSearchPara,
                ...para
            }
            let modalTitle = '';
            let isRowClick = true;
            let clickParams = {isOperatorChart};
            let module = 1;
            if (isOperatorChart) {
                modalTitle = getLabel(384874, '人员耗时清单');
                clickParams.module = 4;
                module = 3;
            } else {
                if (this.iswf) {
                    modalTitle = getLabel(384873, '流程节点耗时清单');
                    isRowClick = false;
                    module = 2;
                } else {
                    modalTitle = getLabel(384872, '流程路径耗时清单');
                    clickParams.module = 2;
                    module = 1;
                }
            }
            dataParams.module = module;
            this.modalParams = {
                modalVisible: true,
                modaltitle: modalTitle,
                isRowClick,
                dataParams,
                clickParams
            }
        } else if (type == 2) {
            const {isOperatorChart = false, key = '', title = '', module = ''} = para;
            if (isOperatorChart) {
                const dataParams = {
                    'api': 'getFlowTimeAnalyseDetailList',
                    ...this.advanceSearchPara,
                    operatorid: key,
                    module: 4
                }
                const modalTitle = getLabel(384875, '人员耗时明细') + '：' + title;
                this.modalParams = {
                    modalVisible: true,
                    modaltitle: modalTitle,
                    dataParams
                }
            } else {
                if (!this.iswf) {
                    this.iswf = true;
                    this.updateAdvanceSearchPara({workflowid: key, workflowname: title, wftypeid: -1});
                }
            }
        } else if (type == 3) {
            const dataParams = {
                'api': 'getFlowTimeAnalyseRequestList',
                ...this.advanceSearchPara,
                ...para
            }
            const modalTitle = getLabel(383302, '具体流程清单');
            this.modalParams = {
                modalVisible: true,
                modaltitle: modalTitle,
                dataParams,
                needAdvacnceSearch: true
            }
        }
    }

    updateModalPara(para = {}) {
        const {keyid = '', title = '', isOperatorChart = false, currentTime} = para;
        if (isOperatorChart) {
            const dataParams = {
                'api': 'getFlowTimeAnalyseDetailList',
                ...this.advanceSearchPara,
                module: 4,
                operatorid: keyid,
                currentTime,
            }
            const modaltitle = getLabel(384875, '人员耗时明细') + '：' + title;
            this.modalParams = {
                modalVisible: true,
                modaltitle,
                dataParams
            }
        } else {
            this.closeModal();
            this.iswf = true;
            this.updateAdvanceSearchPara({workflowid: keyid, workflowname: title, wftypeid: -1});
        }
    }

    closeModal() {
        this.modalParams = {modalVisible: false};
    }

    goBack() {
        this.iswf = false;
        this.updateAdvanceSearchPara({workflowid: '', workflowname: ''});
    }

    clearStatus() {
        this.loading = false;
        this.advanceSearchPara = {
            dateType: 1,
            startDate: '',
            endDate: '',
            wftypeid: -1,
            workflowid: -1,
            workflowname: ''
        };
        this.iswf = false;
        this.modalPara = {};
        this.advanceSearchData = {};
        this.analyseData = {};
    }
}